
import { reqC1List, reqC2List,reqC3List } from "@/api/product/attr/attr";
import { defineStore } from "pinia";
import type { categoryStoreStateType } from "./interface/category";

const useCategoryStore = defineStore('category', {
    // 存储数据的地方
    state: (): categoryStoreStateType => {
        return {
            // 一级分类
            C1List: [],
            // 一级分类的id
            C1Id: '',
            // 二级分类
            C2List: [],
            // 二级分类的id
            C2Id: '',
            // 三级分类
            C3List: [],
            // 三级分类的id
            C3Id: ''
        }
    },

    // 处理数据
    actions: {
        // 获取一级分类的数据
        async getC1List() {
            let result = await reqC1List()
            this.C1List = result
        },
        // 获取二级分类
        async getC2List() {
            // 清空二级和三级分类的id
            this.C2Id = ''
            this.C3Id = ''
            // 清空二级和三级分类的内容
            this.C2List = []
            this.C3List = []
            let result = await reqC2List(this.C1Id)
            this.C2List = result

        },
        // 获取三级分类
        async getC3List() {
            // 清空三级分类的id
            this.C3Id = ''
            // 清空三级分类的内容
            this.C3List = []
            let result = await reqC3List(this.C2Id)
            this.C3List = result

        }
    }
})


export default useCategoryStore